﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Events;
using uTorrentNetClient.ModelDesign.Contracts;
using uTorrentNetClient.ModelDesign.Events;
using uTorrentNetClient.ModelDesign.Helper;

namespace uTorrentNetClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class ShellView : Window, IShellView
    {
        private readonly IEventAggregator _eventAggregator;
        private readonly INotify _notify;
        private readonly INotifyWindowView _notifyWindowView;

        private bool clossing = false;

        public ShellView(IEventAggregator eventAggregator, INotify notify, INotifyWindowView notifyWindowView)
        {
            _eventAggregator = eventAggregator;
            _notify = notify;
            _notifyWindowView = notifyWindowView;
            _notify.ShellView = this;
            InitializeComponent();

            this.StateChanged += new EventHandler(ShellView_StateChanged);
            this.Closing += new System.ComponentModel.CancelEventHandler(ShellView_Closing);
        }

        void ShellView_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _notifyWindowView.CloseView();
            _notifyWindowView.Destroy();
            _notify.CloseIcon();
        }

        void ShellView_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
                this.ShowInTaskbar = false;
                _eventAggregator.GetEvent<ChangeWindowStateEvent>().Publish(this.WindowState);
            }
            else
            {
                this.ShowInTaskbar = true;
                _eventAggregator.GetEvent<ChangeWindowStateEvent>().Publish(this.WindowState);
                this.Topmost = true;
                Thread.Sleep(10);
                this.Topmost = false;
            }
        }


        public void ShowView()
        {
            Show();
            _notifyWindowView.ShowView();
        }

        public void CloseView()
        {
            Close();
        }

        
    }
}
